package top.eggcode.modules.dict.service;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.eggcode.component.mds.DsSwitch;
import top.eggcode.component.mds.Source;
import top.eggcode.component.query.model.PageList;
import top.eggcode.modules.dict.model.DictEntity;
import top.eggcode.modules.dict.model.DictQuery;
import top.eggcode.modules.dict.model.WordEntity;
import top.eggcode.modules.dict.store.DictRepository;
import top.eggcode.modules.dict.store.DictStore;
import top.eggcode.modules.dict.store.WordRepository;
import top.eggcode.base.BaseMarkServiceImpl;

import java.util.List;
import java.util.Optional;

/**
 * Title:
 * Description: TODO
 * Date: 2021/5/21 20:44
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Service
public class DictService extends BaseMarkServiceImpl<DictEntity> {

    private final DictRepository selfRepository;

    private final WordRepository wordRepository;

    private final DictStore selfAccessor;

    public Optional<DictEntity> findByCode(String code) {
        return Optional.ofNullable(selfRepository.findByCode(code));
    }

    public List<WordEntity> getData(String code) {
        DictEntity dict = findByCode(code).get();
        return wordRepository.findByDictId(dict.getId());
    }

    @DsSwitch(Source.SLAVE)
    public PageList<DictEntity> page(DictQuery query) {
        return selfAccessor.page(query);
    }
}
